﻿var AdvertManagement = {};
$(function () {
    //$(":checkbox").each(function () {
    //    if ($(this).attr('status') == "1") {
    //        if ($(this).attr('propertystatusname') != "有效") {
    //            $(this).attr('disabled', 'disabled');
    //        }
    //    }
    //});

    //批量下架
    $("a[name='btnBatchDown']").unbind("click");
    $("a[name='btnBatchDown']").bind("click", function () {
        var objCheck = other.checkCount(true, "#gdList input[ad=ad]:checked", "没有选择任何数据！", "", function () {
            $.OKCancelDialogAgency('<center>您确定要下架吗？</center>', '下架提示', true, function () {
                var tempAds = $("#gdList input[ad=ad]:checked");
                AdvertManagement.checkAdKeyIds = "";
                for (var i = 0; i < tempAds.length; i++) {
                    if (i == 0) {
                        AdvertManagement.checkAdKeyIds += $(tempAds[i]).attr("keyid");
                        continue;
                    }
                    AdvertManagement.checkAdKeyIds += "," + $(tempAds[i]).attr("keyid");
                }
                var url = "/Advert/batch-down-propertyad";
                var reqData = {
                    "adKeyIds": AdvertManagement.checkAdKeyIds
                };
                $.AjaxAgency(url, reqData, function (data) {
                    if (data == "ok") {
                        RefreshNumber();
                        $("#searchForm").submit();
                    }
                    else {
                        $.OKDialogAgency('<center>' + data + '</center>');
                    }
                });
            }, function () {

            });
        });
    });

    //批量删除
    $("a[name='btnBatchDelete']").unbind("click");
    $("a[name='btnBatchDelete']").bind("click", function () {
        var objCheck = other.checkCount(true, "#gdList input[ad=ad]:checked", "没有选择任何数据！", "", function () {
            $.OKCancelDialogAgency('<center>您确定要删除吗？</center>', '删除提示', function () { }, function () {
                var tempAds = $("#gdList input[ad=ad]:checked");
                AdvertManagement.checkAdKeyIds = "";
                for (var i = 0; i < tempAds.length; i++) {
                    if (i == 0) {
                        AdvertManagement.checkAdKeyIds += $(tempAds[i]).attr("keyid");
                        continue;
                    }
                    AdvertManagement.checkAdKeyIds += "," + $(tempAds[i]).attr("keyid");
                }
                var url = "/Advert/batch-remove-propertyad";
                var reqData = {
                    "adKeyIds": AdvertManagement.checkAdKeyIds
                };
                $.AjaxAgency(url, reqData, function (data) {
                    if (data == "ok") {
                        RefreshNumber();
                        $("#searchForm").submit();
                    }
                    else {
                        $.OKDialogAgency('<center>' + data + '</center>');
                    }
                });
            }, function () {

            });
        });
    });

    //批量上架
    $("a[name='btnBatchUp']").unbind("click");
    $("a[name='btnBatchUp']").bind("click", function () {
        var objCheck = other.checkCount(true, "#gdList input[ad=ad]:checked", "没有选择任何数据！", "", function () {
            var tempAds = $("#gdList input[ad=ad]:checked");
            var countPropertyAd = $("#CountPropertyAd").val();
            if (parseInt(countPropertyAd) >= tempAds.length) {
                AdvertManagement.checkAdKeyIds = "";
                var adnos = "";
                var isTradeType = "";
                for (var i = 0; i < tempAds.length; i++) {
                    if (i == 0) {
                        AdvertManagement.checkAdKeyIds += $(tempAds[i]).attr("keyid");
                        if ($(tempAds[i]).attr("propertystatusname") != "有效") {
                            adnos += $(tempAds[i]).attr("adno");
                        }
                        if (($(tempAds[i]).attr("trusttype") != $(tempAds[i]).attr("tradetype")) && ($(tempAds[i]).attr("trusttype") != 3)) {
                            isTradeType += "0";
                        }
                        continue;
                    }
                    AdvertManagement.checkAdKeyIds += "," + $(tempAds[i]).attr("keyid");
                    if ($(tempAds[i]).attr("propertystatusname") != "有效") {
                        adnos += $(tempAds[i]).attr("adno") + ",";
                    }
                    if (($(tempAds[i]).attr("trusttype") != $(tempAds[i]).attr("tradetype")) && ($(tempAds[i]).attr("trusttype") != 3)) {
                        isTradeType += "0" + ",";
                    }
                }
                if (adnos.length > 0) {
                    $.OKDialogAgency('<center>上架广告中有非有效房源，无法上架！</center>');
                    return false;
                }

                if (isTradeType.length > 0) {
                    $.OKDialogAgency('<center>上架广告中有和房源交易类型不一致的房源，无法上架！</center>');
                    return false;
                }
                var url = "/Advert/batch-up-propertyad";
                var reqData = {
                    "adKeyIds": AdvertManagement.checkAdKeyIds
                };
                $.AjaxAgency(url, reqData, function (data) {
                    if (data == "ok") {
                        RefreshNumber();
                        $("#searchForm").submit();
                        $.OKDialogAgency('<center>上架成功！</center>');

                    }
                    else {
                        $.OKDialogAgency('<center>' + data + '</center>');
                    }
                });
            }
            else {
                $.OKDialogAgency('<center>超过可放盘最大数，最多还可放盘' + countPropertyAd + '个！</center>');
            }
        });
    })
  
    //批量设置笋盘
    $("a[name='btnBatchSetHotProperty']").unbind("click");
    $("a[name='btnBatchSetHotProperty']").bind("click", function () {
        var objCheck = other.checkCount(true, "#gdList input[ad=ad]:checked", "没有选择任何数据！", "", function () {
            $.OKCancelDialogAgency('<center>您确定要设置优房吗？</center>', '设置优房提示', true, function () {
                $(".aui_close").click();
                var tempAds = $("#gdList input[ad=ad]:checked");
                AdvertManagement.checkAdKeyIds = "";
                var ishotproperty = "";
                var adNo = "";
                var adNo2 = "";
                for (var i = 0; i < tempAds.length; i++) {
                    // 标题长度小于15无法设置优房
                    if ($(tempAds[i]).attr("title").length < 15) {
                        adNo += $(tempAds[i]).attr("adno") + ",";

                    }
                    var len = parseInt($(tempAds[i]).attr("priceadvantage").length) + parseInt($(tempAds[i]).attr("description").length) + parseInt($(tempAds[i]).attr("otherhighlights").length) + parseInt($(tempAds[i]).attr("rentreason").length);
                    //广告描述4个框的字数之和小于200无法设置优房
                    if (len<200) {
                        adNo2 += $(tempAds[i]).attr("adno") + ",";
                    }
                    if (i == 0) {
                        AdvertManagement.checkAdKeyIds += $(tempAds[i]).attr("keyid");

                        if ($(tempAds[i]).attr("ishotproperty") == "true" || $(tempAds[i]).attr("ishotproperty") == "True") {
                            ishotproperty += $(tempAds[i]).attr("keyid");
                        }
                        continue;
                    }
                    AdvertManagement.checkAdKeyIds += "," + $(tempAds[i]).attr("keyid");
                    if ($(tempAds[i]).attr("ishotproperty") == "true" || $(tempAds[i]).attr("ishotproperty") == "True") {
                        ishotproperty += $(tempAds[i]).attr("keyid") + ",";
                    }

                }             
                //alert(adNo); return false;
                if (ishotproperty.length > 0) {
                    $.OKDialogAgency('<center>所选房源包含优房，无法再次设置优房！</center>');                    
                    return false;                   
                }
                //标题必须>=15字 
                //房源描述必须>=200字
                if (adNo != "") {
                    adNo = adNo.substr(0, adNo.length - 1);
                    $.OKDialogAgency('<center>广告编号为:' + adNo + '的广告,房源标题不足15字，无法设置优房！</center>');
                  return false;                   
                }
                if (adNo2 != "") {
                    adNo2 = adNo2.substr(0, adNo2.length - 1);
                    $.OKDialogAgency('<center>广告编号为:' + adNo2 + '的广告,广告描述总字数不足200字，无法设置优房！</center>');
                    return false;
                }
                var url = "/Advert/batch-set-hotproperty";
                var reqData = {
                    "adKeyIds": AdvertManagement.checkAdKeyIds
                };
                $.AjaxAgency(url, reqData, function (data) {
                    if (data == "ok") {
                        $.OKDialogAgency('<center>设置优房成功！</center>');
                        $("#searchForm").submit();

                        //更新
                        $.AjaxAgency('/advert/RefreshNumber', null, function (data) {
                            if (data) {
                                $("#CountPropertyAd").val(data["CountPropertyAd"]);
                                var packCount = data["CountIsPropertyAd"] + "/ " + data["PackageCount"];
                                $("#AdmPackageCount").html(packCount);
                                var hotCount = data["CountIsHotPropertyAd"] + "/ " + data["CountHotPropertAd"];
                                $("#AdmExcellentRoomCount").html(hotCount);
                                $("#CountSaleProperty").html(data["CountSaleProperty"]);
                                $("#CountRentProperty").html(data["CountRentProperty"]);
                                $("#CountStayShelfArea").html(data["CountStayShelfArea"]);
                                $("#CountPropertyAdCloneHistory").html(data["CountPropertyAdCloneHistory"]);
                            }
                        });
                    }
                    else {
                        $.OKDialogAgency('<center>' + data + '</center>');
                    }
                });
            }, function () {

            });
        });
    });

    //批量取消笋盘
    $("a[name='btnBatchCalcelHotProperty']").unbind("click");
    $("a[name='btnBatchCalcelHotProperty']").bind("click", function () {
        var objCheck = other.checkCount(true, "#gdList input[ad=ad]:checked", "没有选择任何数据！", "", function () {
            $.OKCancelDialogAgency('<center>您确定要取消优房吗？</center>', '取消优房提示', true, function () {
                $(".aui_close").click();
                var tempAds = $("#gdList input[ad=ad]:checked");
                AdvertManagement.checkAdKeyIds = "";
                var ishotproperty = "";
                for (var i = 0; i < tempAds.length; i++) {
                    if (i == 0) {
                        AdvertManagement.checkAdKeyIds += $(tempAds[i]).attr("keyid");
                        if ($(tempAds[i]).attr("ishotproperty") == "false" || $(tempAds[i]).attr("ishotproperty") == "False") {
                            ishotproperty += $(tempAds[i]).attr("keyid");
                        }
                        continue;

                    }
                    if ($(tempAds[i]).attr("ishotproperty") == "false" || $(tempAds[i]).attr("ishotproperty") == "False") {
                        ishotproperty += $(tempAds[i]).attr("keyid") + ",";
                    }
                    AdvertManagement.checkAdKeyIds += "," + $(tempAds[i]).attr("keyid");
                }
                if (ishotproperty.length > 0) {
                    $.OKDialogAgency('<center>所选房源包含不是优房房源，无法取消优房！</center>');
                    return false;
                }
                var url = "/Advert/batch-calcel-hotproperty";
                var reqData = {
                    "adKeyIds": AdvertManagement.checkAdKeyIds
                };
                $.AjaxAgency(url, reqData, function (data) {
                    if (data == "ok") {
                        $.OKDialogAgency('<center>取消优房成功！</center>');
                        $("#searchForm").submit();
                        //更新
                        $.AjaxAgency('/advert/RefreshNumber', null, function (data) {
                            if (data) {
                                $("#CountPropertyAd").val(data["CountPropertyAd"]);
                                var packCount = data["CountIsPropertyAd"] + "/ " + data["PackageCount"];
                                $("#AdmPackageCount").html(packCount);
                                var hotCount = data["CountIsHotPropertyAd"] + "/ " + data["CountHotPropertAd"];
                                $("#AdmExcellentRoomCount").html(hotCount);
                                $("#CountSaleProperty").html(data["CountSaleProperty"]);
                                $("#CountRentProperty").html(data["CountRentProperty"]);
                                $("#CountStayShelfArea").html(data["CountStayShelfArea"]);
                                $("#CountPropertyAdCloneHistory").html(data["CountPropertyAdCloneHistory"]);
                            }
                        });
                    }
                    else {
                        $.OKDialogAgency('<center>' + data + '</center>');
                    }
                });
            }, function () {

            });
        });
    });

    other.selectAll("input[name='chbSelectAll']", "#gdList .checkbox-vertical-middle", AdvertManagement.SelectAllChange);
    //批量克隆下拉框
    //$(".batch a").click(function () {
    //    $(this).next().toggle();
    //    other.showAndHide("#linkBatchClone", "popBatchClone");
    //    $(this).find("i").toggleClass("on");
    //});
    //多对一弹框
    $(".btn-more-to-one-clone").bind("click", function () {
        $.dialog({
            id: "MoreToOneClone",
            lock: true,
            title: "多对一克隆",
            content: $("#MoreToOneClone").html()
        });
    });
    //多对多弹框
    $(".btn-more-to-more-clone").bind("click", function () {
        $.dialog({
            id: "MoreToMoreClone",
            lock: true,
            title: "多对多克隆",
            ok: true,
            cancel: true,
            width: 400,
            content: $("#MoreToMoreClone").html()
        });
    });

    //浮动的工具栏 yjj添加
    var state1 = true; //存储顶部显示的状态
    $(window).scroll(function () {
        if ($(window).scrollTop() > $("#ajaxPages").offset().top) {
            if (state1) {
                //执行浮动层出现后的代码
                $(".batch").addClass("float-edit-toos");
                $(".fixed-div").show();
                if ($("#btnCloneHistory").attr("class") == "current") {
                    $(".fixed-div").css("top", "0");
                }
            }
            state1 = false;
        } else {
            if (!state1) {
                //执行浮动层出现前的代码
                $(".batch").removeClass("float-edit-toos");
                $(".fixed-div").hide();
            }
            state1 = true;
        }
    });
    var n = $("#gdList th:visible").size();
    for (var i = 0; i < n; i++) {
        $(".fixed-div div").append("<span class='fixed-span'></span>");
        $(".fixed-div div").find(".fixed-span").eq(i).html($("#gdList th").eq(i).html());
        $(".fixed-div div").find(".fixed-span").eq(i).css({ "width": $("#gdList th:visible").eq(i).outerWidth(), "text-align": "center" });
    }
});

AdvertManagement.Refresh = function (refresh) {
    var keyId = $(refresh).attr("keyid");
    var url = "/Advert/batch-refreshtime-propertyad";
    var reqData = {
        "keyId": keyId
    };
    $.AjaxAgency(url, reqData, function (data) {
        if (data == "ok") {
            $("#searchForm").submit();
        }
        else {
            $.OKDialogAgency('<center>' + data + '</center>');
        }
    });
};
//勾选checkbox删除按钮变化
AdvertManagement.SelectAllChange = function () {
    var checkedCount = 0;
    $("#gdList input[ad=ad]").each(function () {
        if ($(this).attr("checked") == "checked") {
            checkedCount = checkedCount + 1;
        }
    });
    //如果列表中都勾选了，则将全选设为勾选状态
    if ($("#gdList input[ad=ad]").length == checkedCount) {
        $("input[name='chbSelectAll']").attr("checked", true);
    }
    else {
        $("input[name='chbSelectAll']").attr("checked", false);
    }
}

$("a[name='cloneAnJuKe']").unbind("click");
$("a[name='cloneAnJuKe']").bind("click", function () {
    var KeyId = $(this).attr('KeyId');
    var EstateKeyId = $(this).attr('EstateKeyId');
    $.OKCancelDialogAgency('<center>您确定要将该房源克隆到安居客吗？</center>', '克隆提示', function () { }, function () {
        $.AjaxAgency("get-anjuke-brokerid", null, function (data) {
            if (data == "0") {
                $.MoreButtonDialogAgency("提示", "未绑定安居客帐号", [
                    {
                        name: '绑定帐号', callback: function () {
                            window.open('/center/my-setting');
                        }
                    },
                    {
                        name: '取消', callbakc: function () { }
                    }
                ]);
            } else {

                var url = "/Advert/clone-anjukeadvert";
                var reqData = {
                    "keyId": KeyId,
                    "EstateKeyId": EstateKeyId,
                    "BrokerId": data
                };
                $.AjaxAgency(url, reqData, function (data) {
                    if (data.Status == "ok") {
                        $.OKDialogAgency('<center>' + data.Message + '</center>');
                        RefreshNumber();
                    }
                    else {
                        $.OKDialogAgency('<center>' + data.Message + '</center>');
                    }
                });

            }
        });


    });
});

//克隆搜房
$("a[name='cloneSouFun']").unbind("click");
$("a[name='cloneSouFun']").bind("click", function () {
    var KeyId = $(this).attr('KeyId');
    var EstateKeyId = $(this).attr('EstateKeyId');
    $.OKCancelDialogAgency('<center>您确定要将该房源克隆到搜房吗？</center>', '克隆提示', function () { }, function () {
        $.AjaxAgency("get-brokerid", { "portTypeName": "搜房" }, function (data) {
            if (data == "" || data == "0") {
                $.MoreButtonDialogAgency("提示", "未绑定搜房账号", [
                    {
                        name: '绑定帐号', callback: function () {
                            window.open('/center/my-setting');
                        }
                    },
                    {
                        name: '取消', callbakc: function () { }
                    }
                ]);
            } else {
                var url = "/Advert/clone-soufunadvert";
                var reqData = {
                    "keyId": KeyId,
                    "EstateKeyId": EstateKeyId,
                    "agentId": data
                };
                $.AjaxAgency(url, reqData, function (data) {
                    if (data == "ok") {
                        $.OKDialogAgency('<center>克隆到搜房成功！</center>');
                        RefreshNumber();
                    }
                    else {
                        $.OKDialogAgency('<center >' + data + '</center>');
                    }
                });
            }
        });

    });
});

//克隆新浪
$("a[name='cloneSina']").unbind("click");
$("a[name='cloneSina']").bind("click", function () {
    var KeyId = $(this).attr('KeyId');
    var EstateKeyId = $(this).attr('EstateKeyId');
    $.OKCancelDialogAgency('<center>您确定要将该房源克隆到新浪吗？</center>', '克隆提示', function () { }, function () {
        $.AjaxAgency("get-user-portaccount", { "portTypeName": "新浪" }, function (data) {
            if (data == null || data == "") {
                $.MoreButtonDialogAgency("提示", "未绑定新浪账号", [
                   {
                       name: '绑定帐号', callback: function () {
                           window.open('/center/my-setting');
                       }
                   },
                   {
                       name: '取消', callbakc: function () { }
                   }
                ]);
            } else {

                var url = "/Advert/clone-sinaadvert";
                var reqData = {
                    "keyId": KeyId,
                    "EstateKeyId": EstateKeyId
                };
                $.AjaxAgency(url, reqData, function (data) {
                    if (data == "ok") {
                        $.OKDialogAgency('<center>克隆到搜房成功！</center>');
                        RefreshNumber();
                    }
                    else {
                        $.OKDialogAgency('<center>' + data + '</center>');
                    }
                });
            }
        });

    });
});



//克隆365 Wk to do 
function clone365(url, keyid) {
    var reqData = {
        'url': url,
        'advertKeyId': keyid
    };
    $.AjaxAgency("/Advert/Clone365", reqData, function (data) {

        if (data == "OK") {
            //$.dialog({
            //    id: "clone365",
            //    lock: true,
            //    title: "克隆到365",
            //    background: "#808080",
            //    opacity: 0.87,
            //    content: ChangeCodeToMsg(data)
            //});
            $.OKDialogAgency('<center>克隆到安居客成功！</center>');
        }
        else {
            $.OKDialogAgency('<center >' + data + '</center>');
        }

    });
}


function cloneTaoBao(url, keyid) {
    var reqData = {
        'Url': url,
        'KeyId': keyid
    };
    $.AjaxAgency('/advert/clonetaobao', reqData, function (data) {
        if (data) {
            $.dialog({
                id: "clonetaobao",
                lock: true,
                title: "克隆到淘宝",
                background: "#808080",
                opacity: 0.87,
                content: ChangeCodeToMsg(data)
            });
        }

    });
}

//克隆淘宝时，将返回码转为对应的信息提示
function ChangeCodeToMsg(returnCode) {
    var returnText = "未知错误";
    var arrCocde = [100, 101, 102, 103, 104, 105, 106, 107];
    var arryText = ['克隆成功', '放盘站获取房源失败', '该房源已经克隆过', '基础链接关闭(网络问题)', '淘宝房源克隆失败（楼盘不存在、SessionKey过期或其他原因）', '放盘站房源图片获取失败', '放盘站房源编号为空(放盘站房源编号获取失败)', '未知异常'];
    $.each(arrCocde, function (i, val) {
        if (val == returnCode) {
            returnText = arryText[i];
            return false;
        }
    });
    return returnText;
}

//克隆到58同城
$("a[name='cloneTongCheng']").unbind("click");
$("a[name='cloneTongCheng']").bind("click", function () {
    var keyId = $(this).attr('KeyId');
    var propertyKeyid = $(this).attr("propertyKeyid");
    $.OKCancelDialogAgency('<center>您确定要将该房源克隆到58同城吗？</center>', '克隆提示', function () { }, function () {
        var url = "/Advert/clone-property-to-tong-cheng-ajax";
        var reqData = {
            "KeyId": keyId,
            "propertyKeyid": propertyKeyid
        };
        $.AjaxAgency(url, reqData, function (data) {
            if (data == "ok") {
                $.OKDialogAgency('<center>克隆到58同城成功！</center>');
                RefreshNumber();
            } else if (data.indexOf("@http") != -1) {
                //没有权限认证跳转到58同城登录页
                $.OKDialogAgency('<center><a href="' + data.split("@")[1] + '"> 请点击我跳转到58获取通行证书！ </a></center>');

            } else {
                $.OKDialogAgency('<center >' + data + '</center>');
            }

        });
    });
});


//克隆到兔博士
$("a[name='cloneTuBoShi']").unbind("click");
$("a[name='cloneTuBoShi']").bind("click", function () {
    var KeyId = $(this).attr('KeyId');
    var EstateKeyId = $(this).attr('EstateKeyId');
    $.OKCancelDialogAgency('<center>您确定要将该房源克隆到兔博士吗？</center>', '克隆提示', function () { }, function () {

        var url = "/Advert/clone-tuboshi";
        var reqData = {
            "keyId": KeyId,
            "EstateKeyId": EstateKeyId,
            //"BrokerId": data
        };
        $.AjaxAgency(url, reqData, function (data) {
            if (data.ResultNo == "100") {
                $.OKDialogAgency('<center>' + data.Msg + '</center>');
                RefreshNumber();
            }
            else {
                $.OKDialogAgency('<center>' + data.Msg + '</center>');
            }
        });



    });
});

//克隆到百姓网cloneBaiXing
$("a[name='cloneBaiXing']").unbind("click");
$("a[name='cloneBaiXing']").bind("click", function () {
    var KeyId = $(this).attr('KeyId');
    var EstateKeyId = $(this).attr('EstateKeyId');
    $.OKCancelDialogAgency('<center>您确定要将该房源克隆到百姓网吗？</center>', '克隆提示', function () { }, function () {

        var url = "/Advert/clone-baixing";
        var reqData = {
            "keyId": KeyId,
            "EstateKeyId": EstateKeyId,
            //"BrokerId": data
        };
        $.AjaxAgency(url, reqData, function (data) {
            if (data.Code == "200") {
                $.OKDialogAgency('<center>克隆成功</center>');
                RefreshNumber();
            }
            else {
                $.OKDialogAgency('<center>' + data.Desc + '</center>');
            }
        });



    });
});

